{ ****************************************************************************** }
{ * h264tables.pas        by qq600585                                          * }
{ * https://zpascal.net                                                        * }
{ * https://github.com/PassByYou888/zAI                                        * }
{ * https://github.com/PassByYou888/ZServer4D                                  * }
{ * https://github.com/PassByYou888/PascalString                               * }
{ * https://github.com/PassByYou888/zRasterization                             * }
{ * https://github.com/PassByYou888/CoreCipher                                 * }
{ * https://github.com/PassByYou888/zSound                                     * }
{ * https://github.com/PassByYou888/zChinese                                   * }
{ * https://github.com/PassByYou888/zExpression                                * }
{ * https://github.com/PassByYou888/zGameWare                                  * }
{ * https://github.com/PassByYou888/zAnalysis                                  * }
{ * https://github.com/PassByYou888/FFMPEG-Header                              * }
{ * https://github.com/PassByYou888/zTranslate                                 * }
{ * https://github.com/PassByYou888/InfiniteIoT                                * }
{ * https://github.com/PassByYou888/FastMD5                                    * }
{ ****************************************************************************** }

unit h264tables;

{$INCLUDE zDefine.inc}

interface

uses h264Types;

type
  vlc_bits_len = array [0 .. 1] of uint8_t; // bit pattern, pattern length

  (* ******************************************************************************
    CBP mapping:
    Table 9-4 – Assignment of codeNum to values of coded_block_pattern for macroblock prediction modes
  *)
  { coded_block_pattern Intra_4x4 -> codeNum
  }
const
  tab_cbp_intra_4x4_to_codenum: array [0 .. 47] of uint8_t = (
    3, 29, 30, 17, 31, 18, 37, 8, 32, 38, 19, 9, 20, 10, 11, 2, 16, 33, 34, 21,
    35, 22, 39, 4, 36, 40, 23, 5, 24, 6, 7, 1, 41, 42, 43, 25, 44, 26, 46, 12,
    45, 47, 27, 13, 28, 14, 15, 0
    );

  tab_cbp_inter_4x4_to_codenum: array [0 .. 47] of uint8_t = (
    0, 2, 3, 7, 4, 8, 17, 13, 5, 18, 9, 14, 10, 15, 16, 11, 1, 32, 33, 36,
    34, 37, 44, 40, 35, 45, 38, 41, 39, 42, 43, 19, 6, 24, 25, 20, 26, 21, 46, 28,
    27, 47, 22, 29, 23, 30, 31, 12
    );

  (* ******************************************************************************
    Table 8-13 – Specification of QPC as a function of qPI
    0..29: qpc = qp
  *)
  tab_qp_chroma: array [30 .. 51] of uint8_t =
    (29, 30, 31, 32, 32, 33, 34, 34, 35, 35, 36, 36, 37, 37, 37, 38, 38, 38, 39, 39, 39, 39);

  { ******************************************************************************
    coef_token mapping:
    Table 9-5 – coeff_token mapping to TotalCoeff( coeff_token ) and TrailingOnes( coeff_token )

    index: [tablenum, TotalCoeff 0..16, TrailingOnes 0..3]

    tables 0..2 (jvt-d036):
    4+4 bit code/length

    last table is FLC, so we could generate the values in code (jvt-c028):
    N >= 8 : 6 bit FLC xxxxyy, as follows:
    NumCoeff–1 is transmitted in the first 4 bits (xxxx).  The last 2 bits (yy) are used for T1.  Since NumCoeff=0 also has to be represented, the codeword 000011 is used for this.
    For chroma DC, Num-VLC_Chroma_DC is used.

  }
const
  tab_coef_num: array [0 .. 3, 0 .. 16, 0 .. 3] of vlc_bits_len =
    (
    (
    ((1, 1), (0, 1), (0, 1), (0, 1)),
    ((5, 6), (1, 2), (0, 1), (0, 1)),
    ((7, 8), (4, 6), (1, 3), (0, 1)),
    ((7, 9), (6, 8), (5, 7), (3, 5)),
    ((7, 10), (6, 9), (5, 8), (3, 6)),
    ((7, 11), (6, 10), (5, 9), (4, 7)),
    ((15, 13), (6, 11), (5, 10), (4, 8)),
    ((11, 13), (14, 13), (5, 11), (4, 9)),
    ((8, 13), (10, 13), (13, 13), (4, 10)),
    ((15, 14), (14, 14), (9, 13), (4, 11)),
    ((11, 14), (10, 14), (13, 14), (12, 13)),
    ((15, 15), (14, 15), (9, 14), (12, 14)),
    ((11, 15), (10, 15), (13, 15), (8, 14)),
    ((15, 16), (1, 15), (9, 15), (12, 15)),
    ((11, 16), (14, 16), (13, 16), (8, 15)),
    ((7, 16), (10, 16), (9, 16), (12, 16)),
    ((4, 16), (6, 16), (5, 16), (8, 16))
    ),
    (
    ((3, 2), (0, 1), (0, 1), (0, 1)),
    ((11, 6), (2, 2), (0, 1), (0, 1)),
    ((7, 6), (7, 5), (3, 3), (0, 1)),
    ((7, 7), (10, 6), (9, 6), (5, 4)),
    ((7, 8), (6, 6), (5, 6), (4, 4)),
    ((4, 8), (6, 7), (5, 7), (6, 5)),
    ((7, 9), (6, 8), (5, 8), (8, 6)),
    ((15, 11), (6, 9), (5, 9), (4, 6)),
    ((11, 11), (14, 11), (13, 11), (4, 7)),
    ((15, 12), (10, 11), (9, 11), (4, 9)),
    ((11, 12), (14, 12), (13, 12), (12, 11)),
    ((8, 12), (10, 12), (9, 12), (8, 11)),
    ((15, 13), (14, 13), (13, 13), (12, 12)),
    ((11, 13), (10, 13), (9, 13), (12, 13)),
    ((7, 13), (11, 14), (6, 13), (8, 13)),
    ((9, 14), (8, 14), (10, 14), (1, 13)),
    ((7, 14), (6, 14), (5, 14), (4, 14))
    ),
    (
    ((15, 4), (0, 1), (0, 1), (0, 1)),
    ((15, 6), (14, 4), (0, 1), (0, 1)),
    ((11, 6), (15, 5), (13, 4), (0, 1)),
    ((8, 6), (12, 5), (14, 5), (12, 4)),
    ((15, 7), (10, 5), (11, 5), (11, 4)),
    ((11, 7), (8, 5), (9, 5), (10, 4)),
    ((9, 7), (14, 6), (13, 6), (9, 4)),
    ((8, 7), (10, 6), (9, 6), (8, 4)),
    ((15, 8), (14, 7), (13, 7), (13, 5)),
    ((11, 8), (14, 8), (10, 7), (12, 6)),
    ((15, 9), (10, 8), (13, 8), (12, 7)),
    ((11, 9), (14, 9), (9, 8), (12, 8)),
    ((8, 9), (10, 9), (13, 9), (8, 8)),
    ((13, 10), (7, 9), (9, 9), (12, 9)),
    ((9, 10), (12, 10), (11, 10), (10, 10)),
    ((5, 10), (8, 10), (7, 10), (6, 10)),
    ((1, 10), (4, 10), (3, 10), (2, 10))
    ),
    (
    ((3, 6), (0, 1), (0, 1), (0, 1)),
    ((0, 6), (1, 6), (0, 1), (0, 1)),
    ((4, 6), (5, 6), (6, 6), (0, 1)),
    ((8, 6), (9, 6), (10, 6), (11, 6)),
    ((12, 6), (13, 6), (14, 6), (15, 6)),
    ((16, 6), (17, 6), (18, 6), (19, 6)),
    ((20, 6), (21, 6), (22, 6), (23, 6)),
    ((24, 6), (25, 6), (26, 6), (27, 6)),
    ((28, 6), (29, 6), (30, 6), (31, 6)),
    ((32, 6), (33, 6), (34, 6), (35, 6)),
    ((36, 6), (37, 6), (38, 6), (39, 6)),
    ((40, 6), (41, 6), (42, 6), (43, 6)),
    ((44, 6), (45, 6), (46, 6), (47, 6)),
    ((48, 6), (49, 6), (50, 6), (51, 6)),
    ((52, 6), (53, 6), (54, 6), (55, 6)),
    ((56, 6), (57, 6), (58, 6), (59, 6)),
    ((60, 6), (61, 6), (62, 6), (63, 6))
    )
    );

  {
    separate table for nC == -1 (chroma DC)
    3+3 bit code/length
  }
  tab_coef_num_chroma_dc: array [0 .. 4, 0 .. 3] of vlc_bits_len =
    (
    ((1, 2), (0, 1), (0, 1), (0, 1)),
    ((7, 6), (1, 1), (0, 1), (0, 1)),
    ((4, 6), (6, 6), (1, 3), (0, 1)),
    ((3, 6), (3, 7), (2, 7), (5, 6)),
    ((2, 6), (3, 8), (2, 8), (0, 7))
    );

  { ******************************************************************************
    total_zeros
  }
  { Table 9-7 – total_zeros tables for 4x4 blocks with TotalCoeff( coeff_token ) 1 to 7
    index: TotalCoeff( coeff_token ) 1..7, total_zeros 0..15
  }
const
  tab_total_zeros0: array [1 .. 7, 0 .. 15] of vlc_bits_len =
    (
    ((1, 1), (3, 3), (2, 3), (3, 4), (2, 4), (3, 5), (2, 5), (3, 6), (2, 6), (3, 7), (2, 7), (3, 8), (2, 8), (3, 9), (2, 9), (1, 9)),
    ((7, 3), (6, 3), (5, 3), (4, 3), (3, 3), (5, 4), (4, 4), (3, 4), (2, 4), (3, 5), (2, 5), (3, 6), (2, 6), (1, 6), (0, 6), (0, 0)),
    ((5, 4), (7, 3), (6, 3), (5, 3), (4, 4), (3, 4), (4, 3), (3, 3), (2, 4), (3, 5), (2, 5), (1, 6), (1, 5), (0, 6), (0, 0), (0, 0)),
    ((3, 5), (7, 3), (5, 4), (4, 4), (6, 3), (5, 3), (4, 3), (3, 4), (3, 3), (2, 4), (2, 5), (1, 5), (0, 5), (0, 0), (0, 0), (0, 0)),
    ((5, 4), (4, 4), (3, 4), (7, 3), (6, 3), (5, 3), (4, 3), (3, 3), (2, 4), (1, 5), (1, 4), (0, 5), (0, 0), (0, 0), (0, 0), (0, 0)),
    ((1, 6), (1, 5), (7, 3), (6, 3), (5, 3), (4, 3), (3, 3), (2, 3), (1, 4), (1, 3), (0, 6), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)),
    ((1, 6), (1, 5), (5, 3), (4, 3), (3, 3), (3, 2), (2, 3), (1, 4), (1, 3), (0, 6), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0))
    );

  { Table 9-8 – total_zeros tables for 4x4 blocks with TotalCoeff( coeff_token ) 8 to 15
    index: TotalCoeff( coeff_token ) 8..15, total_zeros 0..8
  }
  tab_total_zeros1: array [8 .. 15, 0 .. 8] of vlc_bits_len =
    (
    ((1, 6), (1, 4), (1, 5), (3, 3), (3, 2), (2, 2), (2, 3), (1, 3), (0, 6)),
    ((1, 6), (0, 6), (1, 4), (3, 2), (2, 2), (1, 3), (1, 2), (1, 5), (0, 0)),
    ((1, 5), (0, 5), (1, 3), (3, 2), (2, 2), (1, 2), (1, 4), (0, 0), (0, 0)),
    ((0, 4), (1, 4), (1, 3), (2, 3), (1, 1), (3, 3), (0, 0), (0, 0), (0, 0)),
    ((0, 4), (1, 4), (1, 2), (1, 1), (1, 3), (0, 0), (0, 0), (0, 0), (0, 0)),
    ((0, 3), (1, 3), (1, 1), (1, 2), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)),
    ((0, 2), (1, 2), (1, 1), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)),
    ((0, 1), (1, 1), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0))
    );

  { Table 9-9 – total_zeros tables for chroma DC 2x2 blocks
    index: TotalCoeff( coeff_token ) 1..3, total_zeros 0..3
  }
  tab_total_zeros_chroma_dc: array [1 .. 3, 0 .. 3] of vlc_bits_len =
    (
    ((1, 1), (1, 2), (1, 3), (0, 3)),
    ((1, 1), (1, 2), (0, 2), (0, 0)),
    ((1, 1), (0, 1), (0, 0), (0, 0))
    );

  { ******************************************************************************
    Table 9-10 – Tables for run_before

    vlc for run_before >= 7 a zeros_left > 6 is generated:
    0001
    00001
    000001
    0000001...
    -> (run_before - 4) x 0  + 1

    index: zeros_left 1..7, run_before 0..6
  }
  tab_run_before: array [1 .. 7, 0 .. 6] of vlc_bits_len =
    (
    ((1, 1), (0, 1), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)),
    ((1, 1), (1, 2), (0, 2), (0, 0), (0, 0), (0, 0), (0, 0)),
    ((3, 2), (2, 2), (1, 2), (0, 2), (0, 0), (0, 0), (0, 0)),
    ((3, 2), (2, 2), (1, 2), (1, 3), (0, 3), (0, 0), (0, 0)),
    ((3, 2), (2, 2), (3, 3), (2, 3), (1, 3), (0, 3), (0, 0)),
    ((3, 2), (0, 3), (1, 3), (3, 3), (2, 3), (5, 3), (4, 3)),
    ((7, 3), (6, 3), (5, 3), (4, 3), (3, 3), (2, 3), (1, 3))
    );

implementation

end.  
 
